<!--
 * @Author: 郭招娣 10615594+guo-zhaodi@user.noreply.gitee.com
 * @Date: 2022-08-17 20:32:08
 * @LastEditors: 郭招娣 10615594+guo-zhaodi@user.noreply.gitee.com
 * @LastEditTime: 2022-08-27 09:30:00
 * @FilePath: \后端管理系统\app\src\views\Users\component\UserTable.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <div>
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column type="index"> </el-table-column>
      <el-table-column prop="username" label="用户名"> </el-table-column>
      <el-table-column prop="email" label="邮箱"> </el-table-column>
      <el-table-column prop="mobile" label="电话"> </el-table-column>
      <el-table-column prop="role_name" label="角色"> </el-table-column>
      <el-table-column prop="mg_state" label="状态">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.mg_state"
            @change="switchUser(scope.row)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="create_time" label="创建时间">
        <template slot-scope="scope">
          {{ scope.row.create_time | newdata }}
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="edit(scope.row)">编辑</el-button>
          <el-button type="text" size="small"  @click="del(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
      <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="userObj.pagenum"
      :page-sizes="[2, 5, 10, 15]"
      :page-size="userObj.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>

<script>
import { httpUsersType } from "../../../api/users";
export default {
  props: {
    // 列表数据
    tableData: {
      type: Array,
      default: () => {
        return [];
      },
    },
    // 参数对象
    userObj: {
      type: Object,
      default: () => {
        return {
          query: "",
          pagenum: 1,
          pagesize: 5,
        };
      },
    },
    // 总条数
    total:{
      type:Number,
      default:0
    }
  },
  methods: {
    handleClick(row) {
      console.log(row);
    },
    // 改变用户状态状态
    async switchUser(obj) {
      const res = await httpUsersType(`users/${obj.id}/state/${obj.mg_state}`);
      if(res.data.meta.status===200){
        this.$message({
          message: res.data.meta.msg,
          type: 'success'
        });
      } else {
        this.$message.error(res.data.meta.msg);
      }
    },
    // 分页
      handleSizeChange(val) {
        this.$emit('page-change',{
          type:'size',
          val
        })
      },
      handleCurrentChange(val) {
        this.$emit('page-change',{
          type:'num',
          val
        })
      },
      // 编辑
      edit(obj){
        this.$emit('edit-dialog',obj);
      },
      // 删除
      del(id){
        console.log(id)
        this.$emit('del-dialog',id)
      }
  },
};
</script>